package src

import (
	. "myTaxServer/src/structs"
	. "myTaxServer/src/tools"
)
var threshold float64 = 5000;

func countTax(d float64) ContTaxReturn { //计算累计税率
	var temp ContTaxReturn
	if(d <= 36000){
		temp.Quick = 0;
		temp.Tax = 0.03;
	}else if(d <= 144000){
		temp.Tax =0.1;
		temp.Quick =2520;
	}else if(d <= 300000){
		temp.Tax =0.2;
		temp.Quick =16920;
	}else if(d <= 420000){
		temp.Tax =0.25;
		temp.Quick =31920;
	}else if(d <= 660000){
		temp.Tax =0.30;
		temp.Quick =52920;
	}else if(d <= 960000){
		temp.Tax =0.35;
		temp.Quick =85920;
	}else if(d > 960000){
		temp.Tax =0.45;
		temp.Quick =181920;
	}else{
		temp.Tax =0;
		temp.Quick =0;
	}
	return temp;
}

func Result(datas MoneyData) [12]ReturnStyle {
	var temp  [12]ReturnStyle;
	var lastMonth, total, alltax, all, lasttotal, sqsyt, zxkct, grsdt, wxyjt, ynssdt, shxzt, Zfgjj,ljynssdt float64 = 0,0,0,0,0,0,0,0,0,0,0,0,0;
	var taxs [12]float64;
	// console.log(datas)
	for i := 0;i < 12;i++ {
		sqsyt=datas.Sqsy[i];//单月税前收入
		Zfgjj=datas.Wxyj[i].Gjjbase*datas.Wxyj[i].Gjj/100;
		zxkct=(datas.Znjy*1000)*datas.Cdjy + datas.Jxjy + datas.Dbyl + datas.Zffd + datas.Zfzj + (2000/(datas.Sylr+1))*datas.Ywlr; //单月专项附加
		wxyjt=Decimal(datas.Wxyj[i].Gjj*datas.Wxyj[i].Gjjbase+datas.Wxyj[i].Sb*datas.Wxyj[i].Sbbase)/100;                          //单月五险一金
		total = (sqsyt - zxkct - wxyjt - threshold) + lasttotal;                                                                   //累计工资
		lasttotal = total;
		taxs[i] = countTax(total).Tax*total - countTax(total).Quick - lastMonth; //套用公式计算本月的税
		lastMonth += taxs[i];
		grsdt=Decimal(taxs[i]);                   //个人所得税
		ynssdt=sqsyt - zxkct - wxyjt - threshold; //个人应纳税所得
		if(sqsyt-zxkct-wxyjt-threshold <0){       //如果出现收入不够起征点的情况，把个人所得税，应纳税所得变为零
			grsdt=0;
			ynssdt=0;
		}
		ljynssdt += ynssdt;
		shxzt=datas.Sqsy[i] - grsdt-wxyjt;//本月税后收入
		alltax += Decimal(grsdt);//总计缴税
		all +=Decimal(shxzt);//总计所得
		temp[i].Sqsr=Decimal(sqsyt);//存到需要返回的数组里面
		temp[i].Zxkc=Decimal(zxkct);
		temp[i].Grsd=Decimal(grsdt);
		temp[i].Wxyj=Decimal(wxyjt);
		temp[i].Ynssd=Decimal(ynssdt);
		temp[i].Shxz=Decimal(shxzt);
		temp[i].Ljynssd=Decimal(ljynssdt);
		temp[i].Zfgjj=Decimal(Zfgjj)
	}
	//fmt.Println(temp);
	return temp;
}